<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\BaseinitController;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class CollectController extends BaseinitController
{
	public function collect(Request $request)
	{
        $id=$request->input('id');
        $openid=$request->input('openid');
        $exit=DB::table('collect')->where('openid',$openid)->where('c_id',$id)->first();
        if($exit){
            if($exit->state==1){
                $data = [
                    'code' => 201,
                    'msg'   => '收藏过了',
                ];
            }elseif($exit->state==2){
                $ok=DB::table('collect')->where('openid',$openid)->where('c_id',$id)->update(['state'=>1]);
                if($ok){
                    $data = [
                        'code' => 202,
                        'msg'   => '收藏成功',
                    ];
                }else{
                    $data = [
                        'code' => 500,
                        'msg'   => '失败',
                    ];
                }
            }
        }else{
            $insertArr=[
                'openid' => $openid,
                'c_id' =>$id,
                'state' =>1,
                'time' =>time(),
                'uptime' =>time()
            ];
            $ok=DB::table('collect')->insert($insertArr);
            if($ok){
                $data = [
                    'code' => 200,
                    'msg'   => '成功',
                ];
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '失败',
                ];
            }
        }
        return response()->json($data);
	}
}